﻿using System;
using System.ComponentModel.Composition;
using System.Windows;
using MediaAssistant.Controls.Dialog;
using MediaAssistant.Controls.Library;
using MediaAssistant.Controls.MessageBox;
using MediaAssistant.Controls.WaitScreen;
using MediaAssistant.Data;
using MediaAssistant.Management;
using MefBasic;
using MefBasic.Commans;
using MefBasic.Threading;
using MediaAssistant.DAL;
using MediaAssistant.DAL.Constants;
using Newtonsoft.Json.Linq;

namespace MediaAssistant.Controls.AddMovie
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class AddMoviePresenter:APresenter<AddMovieView>, IDialogContentPresenter
    {
        [ImportingConstructor]
        public AddMoviePresenter(AddMovieView view) : base(view)
        {
            Movie = new Movie
                        {
                            Id = Guid.NewGuid().ToString(),
                            Rating = 0, 
                            WeightedRating = 0,
                            CreateDate = DateTime.Now
                        };
            SearchIMDbCommand = new DelegateCommand(ExecuteSearchIMDb);
        }

        [Import]
        private WaitScreenPresenter WaitScreen { get; set; }

        public DelegateCommand SearchIMDbCommand { get; set; }

        private void ExecuteSearchIMDb(object obj)
        {
            WaitScreen.Show();

            var job = new Job(SearchIMDb);
            job.Store.Add(Movie);
            job.StartBlocked();

            Genres = job.Store.GetObject<string>("Genre");
            Directors = job.Store.GetObject<string>("Director");
            Writers = job.Store.GetObject<string>("Writer");
            Stars = job.Store.GetObject<string>("Actors");

            WaitScreen.Hide();
        }

        private void SearchIMDb(Job obj)
        {
            var movie = obj.Store.GetObject<Movie>();
            movie.Status = MovieStatus.Creating;
            JObject jsonObject;
            IMDBMovieScanner.ProcessMovie(movie, out jsonObject);
            obj.Store.Add((string)jsonObject.SelectToken("Genre"),"Genre");
            obj.Store.Add((string)jsonObject.SelectToken("Director"), "Director");
            obj.Store.Add((string)jsonObject.SelectToken("Writer"), "Writer");
            obj.Store.Add((string)jsonObject.SelectToken("Actors"), "Actors");
        }

        [Import]
        private IMDBMovieScanner IMDBMovieScanner { get; set; }

        [Import]
        private LibraryDataSource DataSource { get; set; }

        [Import]
        private LibraryPresenter LibraryPresenter { get; set; }

        public bool OnClosing(DialogResult dialogResult)
        {
            if(dialogResult==DialogResult.Ok && string.IsNullOrWhiteSpace(Movie.Title))
            {
                Resolve<MessageBoxPresenter>().Show("Movie title cannot be empty", "Error");
                return false;
            }
            return true;
        }
        public void ShowAddMovie()
        {
            if(Resolve<IDialogPresenter>().ShowDialog("Add Movie", this)==DialogResult.Ok)
            {
                Movie.Status = MovieStatus.Pending;
                DatabaseManager.Instance.AddMovie(Movie);
                DatabaseManager.Instance.UpdateProcessingTitle(DataSource.ShowDrivesMoviesOnly);
                WaitScreen.Hide();
            }
        }

        public Movie Movie
        {
            get { return (Movie)GetValue(MovieProperty); }
            set { SetValue(MovieProperty, value); }
        }

        public static readonly DependencyProperty MovieProperty =
            DependencyProperty.Register("Movie", typeof(Movie), typeof(AddMoviePresenter), new UIPropertyMetadata(null));

        public string Genres
        {
            get { return (string)GetValue(GenresProperty); }
            set { SetValue(GenresProperty, value); }
        }

        public static readonly DependencyProperty GenresProperty =
            DependencyProperty.Register("Genres", typeof(string), typeof(AddMoviePresenter), new UIPropertyMetadata(null));


        public string Directors
        {
            get { return (string)GetValue(DirectorsProperty); }
            set { SetValue(DirectorsProperty, value); }
        }

        public static readonly DependencyProperty DirectorsProperty =
            DependencyProperty.Register("Directors", typeof(string), typeof(AddMoviePresenter), new UIPropertyMetadata(null));


        public string Writers
        {
            get { return (string)GetValue(WritersProperty); }
            set { SetValue(WritersProperty, value); }
        }

        public static readonly DependencyProperty WritersProperty =
            DependencyProperty.Register("Writers", typeof(string), typeof(AddMoviePresenter), new UIPropertyMetadata(null));


        public string Stars
        {
            get { return (string)GetValue(StarsProperty); }
            set { SetValue(StarsProperty, value); }
        }

        public static readonly DependencyProperty StarsProperty =
            DependencyProperty.Register("Stars", typeof(string), typeof(AddMoviePresenter), new UIPropertyMetadata(null));



        
    }
}
